package com.group4.rbac.web.controller;

import com.group4.rbac.entity.User;
import com.group4.rbac.service.UserService;
import com.group4.rbac.util.Audience;
import com.group4.rbac.util.JwtUtil;
import com.study.dto.UserDTO;
import com.study.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/rbac/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    private Audience audience;

    /**
     * 用户登陆
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user) {
        ResponseResult<User> login = userService.login(user); //调用service业务逻辑层
        return login;
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        ResponseResult register = userService.register(user);
        return register;
    }


    /**
     * 用户列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseResult userList() {
        List<User> users = userService.selectAll();
        return new ResponseResult(200, "用户列表", users);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("getById/{id}")
    public ResponseResult<UserDTO> getUserById(@PathVariable int id) {

        User userById = userService.getUserById(id);

        UserDTO userDTO = new UserDTO();

        BeanUtils.copyProperties(userById, userDTO);

        return new ResponseResult(200, "", userDTO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public ResponseResult logout() {

        return ResponseResult.SUCCESS;
    }

    /**
     * 返回信息
     * @return
     */
    @GetMapping("/info")
    public ResponseResult<Map> info() {
        ResponseResult<Map> result = new ResponseResult<>();
        HashMap<String, String> map = new HashMap<>();
        map.put("roles", "角色列表");
        map.put("name", "admin");
        map.put("avatar", "https://group4-edu.oss-cn-beijing.aliyuncs.com/edu-image/cover/R-C.jpg");
        result.setCode(200);
        result.setMessage("suc");
        result.setData(map);
        return result;
    }

    /**
     * jwt解析
     * @param jwt
     * @return
     */
    @RequestMapping("parseJwt")
   public ResponseResult<Integer> parseJWT(@RequestParam("jwt")String jwt){
        boolean result = JwtUtil.verifyJwt(jwt, audience);
        if(!result){
            return new ResponseResult<>(500,"登录过期请重新登陆");
        }
        ResponseResult<Integer> integerResponseResult = userService.parseJWT(jwt);
        return integerResponseResult;
    };

    /**
     * 远程调用测试接口
     * @return
     */
    @RequestMapping("test")
    public ResponseResult test(){
        return new ResponseResult("ok");
    }
}

